package org.sanguine.study.hometasks.second.auxiliary;

public class Point implements Comparable<Point> {
	public final Double x;
	public final Double y;
	
	public Point (Double x, Double y) {
		if (x == null || y == null) throw new IllegalArgumentException("Coordinates must be non-null");
		this.x = x;
		this.y = y;
	}

	@Override
	public int compareTo(Point o) {
		if (x != o.x) return x.compareTo(o.x);
		else return y.compareTo(o.y);
	}
	
	public boolean isCollinear(Point p1, Point p2) {
		if (p1 == null || p2 == null) throw new IllegalArgumentException("Other points must not be null");
		
		return crossProduct(this, p1, p2).equals(0.0);
	}
	
	private Double crossProduct(Point p0, Point p1, Point p2) {
		Double result = (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
		if (result.equals(-0.0)) result = 0.0;
		return result; 
	}

	@Override
	public String toString() {
		return "(" + x + ", " + y + ")";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((x == null) ? 0 : x.hashCode());
		result = prime * result + ((y == null) ? 0 : y.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (!(obj instanceof Point))
			return false;
		Point other = (Point) obj;
		if (x == null) {
			if (other.x != null)
				return false;
		} else if (!x.equals(other.x))
			return false;
		if (y == null) {
			if (other.y != null)
				return false;
		} else if (!y.equals(other.y))
			return false;
		return true;
	}
}
